package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.GoodsType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/***
 * 物品类型信息
 * @author yy
 *
 */
@Service
public class GoodsTypeDAO extends BaseDAO<GoodsType, Integer> {
	
	private OrganDAO organDAO;
	//private final static String sql_findByNameOc = "FROM GoodsType gt WHERE gt.name = ? AND gt.oc = ?";
//	private final static String sql_loadAll = "FROM GoodsType ";
	/***
	 * 查询
	 */
	public RESULT findByMap(Map<String, String> map, PageBean pb) {
		StringBuffer hql = new StringBuffer("FROM GoodsType gt WHERE 1=1");
		List<Object> list = null;
		if(map != null){
			
			list = new ArrayList<Object>();
			String name = map.get(Parm.NAME);
			String oc = map.get(Parm.OC);
			
			if(isNotNull(name)){
				hql.append(" AND gt.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(oc)){
				hql.append(" AND gt.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			
		}
		hql.append(" ORDER BY gt.code DESC");
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
//	/**
//	 * 根据机构编号在缓存中获取对应的机构
//	 * @param	机构编号
//	 */
//	public GoodsType getType(List<Integer> ocs) {
//		if (null == Cache.goodsTypeList || Cache.goodsTypeList.size() == 0)
//			loadAll();
//		return aidGetType(ocs, Cache.goodsTypeList);
//	}
//	public GoodsType aidGetType(List<Integer> ocs, List<GoodsType> list) {
//		if (null == ocs || list.size() == 0)
//			return null;
//		GoodsType result = null;
//		for (GoodsType gt : list) {
//			for (Integer oc : ocs) {
//				if (gt.getOc().equals(oc.intValue())) 
//					return gt;
//				else 
//					result = aidGetType(ocs, gt.getSonList());
//				if (null != result)
//					return result;
//			}
//			
//		}
//		return null;
//	}
	
	/**
	 * 通过根机构编号获取缓存对应的物品类型，并且已经把父子级关系拉好
	 * @param rootCode	根机构编号
	 */
	public List<GoodsType> getAll(Integer rootCode) {
		if (null == Cache.oc_goodsTypeList.get(rootCode)) {
			return loadAll(rootCode, organDAO.getSonCodeList(rootCode));
		}
		return Cache.oc_goodsTypeList.get(rootCode);
	}
	
	// 查询所有类型，并保存其父子级关系
	 public List<GoodsType> loadAll(Integer rootCode, List<Integer> ocs) {
		if (null == ocs || ocs.size() == 0)
			return null;

		StringBuffer hql = new StringBuffer("FROM GoodsType g WHERE g.oc IN (");

		List<Object> parm = new ArrayList<Object>();

		for (Integer i : ocs) {
			hql.append("?,");
			parm.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");

		List<GoodsType> list = find(hql.toString(), parm.toArray());
		if (null != list && list.size() > 0) {

			for (GoodsType gt : list) {
				for (GoodsType son : list) {
					if (null == gt.getSonList())
						gt.setSonList(new ArrayList<GoodsType>());

					if (gt.getCode() == son.getPc())
						gt.getSonList().add(son);
				}
			}

			List<GoodsType> slist = new ArrayList<GoodsType>();
			for (GoodsType gt : list) {
				if (null == gt.getPc() || gt.getPc() == 0) {
					slist.add(gt);
				}
			}
			Cache.oc_goodsTypeList.put(rootCode, slist);
			return slist;
		}
		return null;
	 }
		 
	 /**
	  * 根据类型编号在缓存中获取对应的类型
	  * @param code  类型编号
	  * @return
	  */
	public GoodsType getGoodsType(Integer rootCode, Integer code) {
		if (null == Cache.oc_goodsTypeList.get(rootCode))
			loadAll(rootCode,organDAO.getSonCodeList(rootCode));
		return aidGetGoodsType(code, Cache.oc_goodsTypeList.get(rootCode));
	}
	public GoodsType aidGetGoodsType(Integer code, List<GoodsType> list) {
		if (null == code || null == list || list.size() == 0)
			return null;
		GoodsType result = null;
		for (GoodsType gt : list) {
			if (gt.getCode().equals(code.intValue())) 
				return gt;
			else 
				result = aidGetGoodsType(code, gt.getSonList());
			if (null != result)
				return result;
		}
		return null;
	}
	
	
	public List<GoodsType> findByNameOc(String name, List<Integer> ocs){
		if (name == null || ocs == null) return null;
		StringBuffer hql = new StringBuffer("FROM GoodsType gt WHERE gt.name = ? AND gt.oc IN ( ");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(name);
		for (Integer i : ocs) {
			if (null != i) {
				parm.add(i);
				hql.append("?,");
			}
		}	
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parm.toArray());
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
	public RESULT update(GoodsType gt) {
		if (null == gt || gt.getCode() == null) return null;
		GoodsType db = this.get(gt.getCode());
		if (null == db) return RESULT.NOT_FIND;
		
		boolean ok = false;
		if (!isNotNull(db.getName()) && isNotNull(gt.getName()) || (!db.getName().equals(gt.getName()))) {
			db.setName(gt.getName());
			ok = true;
		}
		
		
		if (ok)
			return super.upd(db);
		
		return RESULT.SUCCESS;
	}
	
}
